package app.netant.finishbook.wanantxt;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import app.netant.NetAntBase;

public class WananTxtNetDetail extends NetAntBase {
	
	private WananTxtEntity entity;

	public WananTxtNetDetail(String netSite) {
		super(netSite);
	}
	
	public void setEntity(WananTxtEntity entity){
		this.entity = entity;
	}
	
	public void analyseTitle(){
		
		Elements activePart = document.select("#content .main_wrap .detail .title h1");
		if(activePart.size()>0){
			String name = activePart.get(0).text();
			
			System.out.println("【Title】"+name);
			
			this.entity.setNovelFullName(name);
			this.entity.setNovelName(name);
			
		}
		
	}
	
	public void analyseTags(){
		
		Elements activePart = document.select("#content .main_wrap .detail ul li");
		if(activePart.size()>0){
			
			for(int i=0;i<activePart.size();i++){
				
				Element child = activePart.get(i);
				
				String tagName = child.select("span").text();
				String tagValue = child.select("p").text();
				
				System.out.println("【TAG】"+tagName + "=" + tagValue);
				
				this.entity.setTag(tagName, tagValue);
				
			}

		}
		
	}
	
	public void analyseMemo(){
		
		Elements activePart = document.select("#content .playlist .mlist");
		if(activePart.size()>0){
			
			String memo = activePart.get(0).text();
			
			System.out.println("【Memo】"+memo);
			this.entity.setNovelMemo(memo);

		}
		
	}
	
	public void analyseType(){
		
		Elements activePart = document.select("#content .breadnav_wrap .breadnav a");
		if(activePart.size()>1){
			
			String type = activePart.get(1).text();
			
			System.out.println("【Type】"+type);
			this.entity.setNovelType(type);

		}
		
	}
	
	
	public void analyseHref(){
		
		Elements activePart = document.select("#content .main_wrap .l_b .pic325 a");
		if(activePart.size()>1){
			
			for(int i=0;i<activePart.size();i++){
				
				String txt = activePart.get(i).text();
				String oldhref = activePart.get(i).attr("href");
				
				if("下载本书".equals(txt)){
					
					int points = oldhref.lastIndexOf("/");
					if(points>0){
						oldhref = oldhref.substring(points+1).trim();
						String number = oldhref.substring(0,oldhref.length()-5);
						String href = "http://www.wanantxt.com/modules/article/txtarticle.php?id="+number;
						System.out.println("【DownHref】"+href);
						this.entity.setDownPath(href);			
					}

					break;
				}
				
			}

		}
		
	}
	
	public void analyse(){
		
		this.analyseTitle();
		this.analyseTags();
		this.analyseMemo();
		this.analyseType();
		this.analyseHref();
		
		this.entity.download();
		this.entity.insert();
		
	}

	public static void main(String[] args) {
		
		WananTxtNetDetail detail = new WananTxtNetDetail("http://www.wanantxt.com/txt/xiazai/down-9649.html");
		detail.analyseTitle();
		detail.analyseTags();
		detail.analyseMemo();
		detail.analyseType();
		detail.analyseHref();
		
	}
	
}
